import { mapGetters, mapActions } from 'vuex'

import { 
	themeList, 
	addCss,
  removeCss,
  getReadTimeByMinute
} from 'utils/book'

import { 
	saveLocation,
  getBookmark
} from 'utils/localStorage'

export const ebookMixin = {
  computed: {
    ...mapGetters([
      'defaultBook',
      'fileName',
      'menuVisible',
      'settingVisible',
      'defaultFontSize',
      'defaultFontFamily',
      'fontFamilyVisible',
      'defaultTheme',
      'bookAvailable',
      'progress',
      'section',
      'isPaginating',
      'currentBook',
      'navigation',
      'cover',
      'metadata',
      'paginate',
      'pagelist',
      'offsetY',
      'isBookmark',
      'bookShow'
    ]),
    themeList() {
			return themeList(this)
		},
    getSectionName() {
      // if (this.section) {
      //  const sectionInfo = this.currentBook.section(this.section)
      //  if (sectionInfo && sectionInfo.href && this.currentBook && this.currentBook.navigation) {
      //    return this.currentBook.navigation.get(sectionInfo.href).label
      //  }
      // }
      return this.section ? this.navigation[this.section].label : ''
    }
  },
  methods: {
    ...mapActions([
      'setDefaultBook',
      'setFileName',
      'setMenuVisible',
      'setSettingVisible',
      'setDefaultFontSize',
      'setDefaultFontFamily',
      'setFontFamilyVisible',
      'setDefaultTheme',
      'setBookAvailable',
      'setProgress',
      'setSection',
      'setIsPaginating',
      'setCurrentBook',
      'setNavigation',
      'setCover',
      'setMetadata',
      'setPaginate',
      'setPagelist',
      'setOffsetY',
      'setIsBookmark',
      'setBookShow'
    ]),
    initGlobalStyle() {
			switch (this.defaultTheme) {
				case 'Default':
					addCss(`ebook/theme/theme_default2.css`)
					break;
				case 'Eye':
					addCss(`ebook/theme/theme_eye2.css`)
					break;
				case 'Gold':
					addCss(`ebook/theme/theme_gold2.css`)
					break;
				case 'Night':
					addCss(`ebook/theme/theme_night2.css`)
					break;
				default:
					addCss(`ebook/theme/theme_default2.css`)
					break;
			}
		},
		refreshLocation() {
			const currentLocation = this.currentBook.rendition.currentLocation()
      if (currentLocation && currentLocation.start) {
        const startCfi = currentLocation.start.cfi
        const progress = this.currentBook.locations.percentageFromCfi(startCfi)
        this.setProgress(Math.floor(progress * 100))
        this.setSection(currentLocation.start.index)
        saveLocation(this.fileName, startCfi)
        // 判断是否显示书签
        const bookmark = getBookmark(this.fileName)
        if (bookmark) {
          if (bookmark.some(item => item.cfi === startCfi)) {
            this.setIsBookmark(true)
          } else {
            this.setIsBookmark(false)
          }
        } else {
          this.setIsBookmark(false)
        }
      }
		},
		display(target, cb) {
			if (target) {
				this.currentBook.rendition.display(target).then(() => {
          this.setBookShow(true)
					this.refreshLocation()
					if (cb) cb()
				})
			} else {
				this.currentBook.rendition.display().then(() => {
          this.setBookShow(true)
					this.refreshLocation()
					if (cb) cb()
				})
			}
		},
    hideTitleAndMenu() {
      this.setMenuVisible(false)
      this.setSettingVisible(-1)
      this.setFontFamilyVisible(false)
    },
    getReadTimeText() {
      return this.$t('book.haveRead').replace('$1', getReadTimeByMinute(this.fileName))
    }
  }
}

